package com.ruoyi.msg.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.msg.domain.bo.MsgDcwjTkBo;
import com.ruoyi.msg.domain.bo.TkFormBo;
import com.ruoyi.msg.domain.vo.MsgDcwjTkVo;
import com.ruoyi.msg.domain.vo.TkXxDetailVo;
import com.ruoyi.msg.service.IMsgDcwjTkService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 题库管理
 *
 * @author xiaohao
 * @date 2023-02-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/msg/dcwjTk")
public class MsgDcwjTkController extends BaseController {

    private final IMsgDcwjTkService iMsgDcwjTkService;


    /**
     * xh-题库-整体详情
     */
    @GetMapping("/detail/{id}")
    public R<TkXxDetailVo> getDetail(@PathVariable Long id) {
        return R.ok(iMsgDcwjTkService.getDetail(id));
    }

    /**
     * xh-整体表单保存 题库+选项 新增or更新
     */
    @RepeatSubmit()
    @PostMapping("/overallSave")
    public R<String> overallSave(@RequestBody TkFormBo bo) {
        if (null == bo) {
            return R.fail("请传入需保存的数据");
        }
        iMsgDcwjTkService.saveOrUpdateForm(bo);
        return R.ok("保存成功");
    }

    /**
     * xh-关联删除
     *
     * @param id 题库id
     */
    @DeleteMapping("/relaDel/{id}")
    public R<String> relaDel(@PathVariable("id") Long id) {
        iMsgDcwjTkService.relaDel(id);
        return R.ok("删除成功");
    }

    /**
     * 查询题库管理列表
     */
    @SaCheckPermission("msg:dcwjTk:list")
    @GetMapping("/list")
    public TableDataInfo<MsgDcwjTkVo> list(MsgDcwjTkBo bo, PageQuery pageQuery) {
        return iMsgDcwjTkService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出题库管理列表
     */
    @SaCheckPermission("msg:dcwjTk:export")
    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MsgDcwjTkBo bo, HttpServletResponse response) {
        List<MsgDcwjTkVo> list = iMsgDcwjTkService.queryList(bo);
        ExcelUtil.exportExcel(list, "题库管理", MsgDcwjTkVo.class, response);
    }

    @GetMapping("/all")
    public R<List<MsgDcwjTkVo>> getAll() {
        return R.ok(iMsgDcwjTkService.queryList(new MsgDcwjTkBo()));
    }

    /**
     * 获取题库管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("msg:dcwjTk:query")
    @GetMapping("/{id}")
    public R<MsgDcwjTkVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iMsgDcwjTkService.queryById(id));
    }

    /**
     * 新增题库管理
     */
    @SaCheckPermission("msg:dcwjTk:add")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MsgDcwjTkBo bo) {
        return toAjax(iMsgDcwjTkService.insertByBo(bo));
    }

    /**
     * 修改题库管理
     */
    @SaCheckPermission("msg:dcwjTk:edit")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MsgDcwjTkBo bo) {
        return toAjax(iMsgDcwjTkService.updateByBo(bo));
    }

    /**
     * 删除题库管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("msg:dcwjTk:remove")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMsgDcwjTkService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
